

/**
 * 私教
 * 
 */
app.controller('privateTeachCtrl', function ($scope, $rootScope, $http, dialogsManager) {


  //私课所有课程
  var url = 'http://qa.aixingfu.net/v1/api-private/get-all-private-class?venueId='+$rootScope.globalInfo.user.venueId
  //私课课种分类
  var urlPrivateCourse = 'http://qa.aixingfu.net/v1/api-class/get-course-data?type=1&venueId='+$rootScope.globalInfo.user.venueId;
  $scope.privateCourse = function () {
    $http({
      url: url,
      method: 'GET',
    }).success(function (date) {
      $scope.items = date;
    }).error(
      function (data) {
        console.log(data);
        $scope.showMessage = function () {
          dialogsManager.showMessage('系统错误,请联系工作人员');
        }
        $scope.showMessage();
      }
      );
  }
  $scope.privateCourse();

  $scope.doRefresh = function () {
    $http.get(url)
      .success(function (newItems) {
        $scope.items = newItems;
      })
      .finally(function () {
        $scope.$broadcast('scroll.refreshComplete');
      });
  };
  //课种分类
  $scope.getPrivateCourseClass = function () {
    $http({
      url: urlPrivateCourse,
      method: 'GET',
    }).success(function (course) {
      $scope.privateClassFication = course;
    }).error(
      function (course) {
        console.log(course);
        $scope.showMessage = function () {
          dialogsManager.showMessage('系统错误,请联系工作人员');
        }
        $scope.showMessage();
      }
      );
  }
  $scope.getPrivateCourseClass();

  //点击显示课程分类列表
  $scope.privateCourseList = function () {
    $('#privateCourseSelect').toggleClass('disNone')
  }
  //选中课程分类中课程后触发的事件
  $scope.privateCourseSelected = function (id) {
    var urlSelect = 'http://qa.aixingfu.net/v1/api-private/get-all-private-class?course=' + id+'&venueId='+$rootScope.globalInfo.user.venueId;
    $http({
      url: urlSelect,
      method: 'GET',
    }).success(function (date) {
      if (date == '') {
        var message = '不好意思暂时没有该课程';
        $scope.showMessage = function () {
          dialogsManager.showMessage(message);
        }
        $scope.showMessage();
      } else if (date != '') {
        $scope.items = date;
      }
    }).error(
      function (data) {
        console.log(data);
        $scope.showMessage = function () {
          dialogsManager.showMessage('系统错误,请联系工作人员');
        }
        $scope.showMessage();
      }
      );
    $('#privateCourseSelect').toggleClass('disNone');
  }
  //获取全部课程
  $scope.allPrivateCourse = function () {
    $('#privateCourseSelect').toggleClass('disNone');
    $scope.privateCourse()
  }
  //关闭课程分类
  $scope.closeCourseSelect = function () {
    $('#privateCourseSelect').addClass('disNone');
  }
  //关闭筛选下拉
  $(function () {
    $('.listTabs').on('click', function () {
      $('#courseSelect').addClass('disNone');
      $('#privateCourseSelect').addClass('disNone');
    })
  })

  $scope.onclick = function (id) {
    $rootScope.idsws = id;
    location.href = '#/tab/privateTeach/' + id;
  }

})
/**
 * 私教子页面
 * 
 */
app.controller('privateTeachChatsCtrl', function ($rootScope, $scope, $stateParams, $http, $ionicHistory, $state, dialogsManager) {
  //返回上一页
  $scope.backPrivate = function () {
    $ionicHistory.goBack();
  }

  var url = 'http://qa.aixingfu.net/v1/api-private/get-private-class-info?id=' + $rootScope.idsws + '&memberId=' + $rootScope.globalInfo.user.ids;

  $http({
    url: url,
    method: 'GET',
  }).success(function (date) {
    $scope.items = date;
  }).error(
    function (data) {
      console.log(data);
      $scope.showMessage = function () {
        dialogsManager.showMessage('系统错误,请联系工作人员');
      }
      $scope.showMessage();
    }
    );

  $rootScope.date = '4';
  // 跳转 产品课程
  $scope.productCourse = function () {
    location.href = '#/tab/privateTeach/' + $rootScope.idsws + '/' + $rootScope.date + '/productCourse';
  }
  // 选择时间
  $scope.ionFooterButton = function () {
    if ($scope.items.product == false) {
      $scope.showMessage = function () {
        dialogsManager.showMessage("您还没有购买这节课！");
      }
      $scope.showMessage();
      $('#leagueDetailsButtons').attr('disabled', 'disabled')
      return
    }
    if ($scope.items.orderId != false) {
      $rootScope.itemsOrderId = $scope.items.orderId
    }
    location.href = '#/tab/privateTeach/' + $rootScope.idsws + '/' + $rootScope.date + '/selectTime';
  }
  //地图 
  $scope.mapClick = function () {
    location.href = '#/tab/privateTeach/:' + $rootScope.idsws + '/map';
  }
})
/**
 * 产品课程
 */
app.controller('productCourseCtrl', function ($rootScope, $scope, $stateParams, $http, dialogsManager) {

  var url = 'http://qa.aixingfu.net/v1/api-private/get-private-detail?id=' + $rootScope.idsws + "&memberId=" + $rootScope.globalInfo.user.ids;

  $http({
    url: url,
    method: 'GET',
  }).success(function (date) {
    $scope.items = date;

    for (var key in $scope.items.packageClass) {
      if ($scope.items.packageClass.hasOwnProperty(key)) {
        if ($scope.items.packageClass[key].status == 1) {
          $scope.aaa = 'ion-checkmark-circled';
        }
        if ($scope.items.packageClass[key].status == 2) {
          $scope.aaa = '';
        }
      }
    }
  }).error(
    function (data) {
      console.log(data);
      $scope.showMessage = function () {
        dialogsManager.showMessage('系统错误,请联系工作人员');
      }
      $scope.showMessage();
    }
    );

})
/**
 * 选择时间 预约教练
 */
app.controller('selectTimeCtrl', function ($rootScope, $scope, $stateParams, $location, $state, $ionicHistory, $http, $sce, $ionicPopup, dialogsManager) {

  $scope.selectDate = getDate();
  var url = 'http://qa.aixingfu.net/v1/api-coach/get-coach?venueId='+$rootScope.globalInfo.user.venueId;

  $http({
    url: url,
    method: 'GET',
  }).success(function (date) {
    $scope.items = date;
  }).error(
    function (data) {
      console.log(data);
      $scope.showMessage = function () {
        dialogsManager.showMessage('系统错误,请联系工作人员');
      }
      $scope.showMessage();
    }
    );


  $scope.selectOrderOne = function ($event, id) {
    $('.selectOrderOne').removeClass('selectOrder')
    $($event.target).addClass('selectOrder')
    // 获取教练id
    $rootScope.coachId = id;
  }


  $scope.data = { result: "" };
  // 定义时间回调函数
  $scope.callback = function (value) {
    // 获取预约时间
    $scope.dates = value;
    return false;
  }



  $rootScope.yuke = function () {
    if ($scope.dates == undefined) {
      var time = getNowFormatDate();
      $scope.dates = time;
    }
    var document = { classId: $rootScope.itemsOrderId, memberId: $rootScope.globalInfo.user.ids, classType: 'charge', coachId: $rootScope.coachId, classDate: $scope.dates }
    if (document) {
      if (document.coachId == '' || document.coachId == undefined || document.coachId != $rootScope.coachId) {
        $scope.showMessage = function () {
          dialogsManager.showMessage("请选择教练");
        }
        $scope.showMessage();
      }
    }

    if (document.coachId != undefined) {
      var url = 'http://qa.aixingfu.net/v1/api-about-record/set-about-record';
      $http({
        method: 'post',
        url: url,
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
        data: $.param(document)
      }).then(function (success) {
        $ionicHistory.clearCache();
        $state.go('tab.completes', {}, { reload: true })
      }, function (error) {
        console.log(error);
        $scope.showMessage = function () {
          dialogsManager.showMessage('系统错误,请联系工作人员');
        }
        $scope.showMessage();
      })

    }
  }
})
// 私课完成预约
app.controller('completesCtrl', function ($rootScope, $ionicHistory, $scope, $stateParams, $state) {

  $scope.backHomepage = function () {
    $state.go('tab.privateTeach', {})
  }


})

/**
 * privateTeachMapCtrl 地图 
 */
app.controller('privateTeachMapCtrl', function ($rootScope, $scope, $stateParams, $http, $ionicHistory) {
  //返回上一页
  $scope.backPrivate = function () {
    $ionicHistory.goBack();
  }

  //创建和初始化地图函数：
  function initMap() {
    createMap();//创建地图
    setMapEvent();//设置地图事件
    addMapControl();//向地图添加控件
    addMarker();//向地图中添加marker
  }


  //创建地图函数：
  function createMap() {
    var map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图
    var point = new BMap.Point(113.676957, 34.763269);//定义一个中心点坐标
    map.centerAndZoom(point, 17);//设定地图的中心点和坐标并将地图显示在地图容器中
    window.map = map;//将map变量存储在全局
  }



  //地图事件设置函数：
  function setMapEvent() {
    map.disableDragging();//禁用地图拖拽事件
    map.disableScrollWheelZoom();//禁用地图滚轮放大缩小，默认禁用(可不写)
    map.disableDoubleClickZoom();//禁用鼠标双击放大
    map.disableKeyboard();//禁用键盘上下左右键移动地图，默认禁用(可不写)
  }

  //地图控件添加函数：
  function addMapControl() {
  }


  //标注点数组
  var markerArr = [{ title: "", content: "我爱运动健身馆大上海城店", point: "113.676957|34.763269", isOpen: 0, icon: { w: 25, h: 25, l: -2, t: 21, x: 6, lb: 5 } }
  ];
  //创建marker
  function addMarker() {
    for (var i = 0; i < markerArr.length; i++) {
      var json = markerArr[i];
      var p0 = json.point.split("|")[0];
      var p1 = json.point.split("|")[1];
      var point = new BMap.Point(p0, p1);
      var iconImg = createIcon(json.icon);
      var marker = new BMap.Marker(point, { icon: myIcon });
      var iw = createInfoWindow(i);
      var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
      marker.setLabel(label);
      map.addOverlay(marker);
      label.setStyle({
        borderColor: "#808080",
        color: "#333",
        cursor: "pointer"
      });

      (function () {
        var index = i;
        var _iw = createInfoWindow(i);
        var _marker = marker;
        _marker.addEventListener("click", function () {
          this.openInfoWindow(_iw);
        });
        _iw.addEventListener("open", function () {
          _marker.getLabel().hide();
        })
        _iw.addEventListener("close", function () {
          _marker.getLabel().show();
        })
        label.addEventListener("click", function () {
          _marker.openInfoWindow(_iw);
        })
        if (!!json.isOpen) {
          label.hide();
          _marker.openInfoWindow(_iw);
        }
      })()
    }
  }
  //创建marker
  function addMarker() {
    for (var i = 0; i < markerArr.length; i++) {
      var json = markerArr[i];
      var p0 = json.point.split("|")[0];
      var p1 = json.point.split("|")[1];
      var point = new BMap.Point(p0, p1);
      var iconImg = createIcon(json.icon);
      var marker = new BMap.Marker(point, { icon: iconImg });
      var iw = createInfoWindow(i);
      var label = new BMap.Label(json.title, { "offset": new BMap.Size(json.icon.lb - json.icon.x + 10, -20) });
      marker.setLabel(label);
      map.addOverlay(marker);
      label.setStyle({
        borderColor: "#808080",
        color: "#333",
        cursor: "pointer"
      });

      (function () {
        var index = i;
        var _iw = createInfoWindow(i);
        var _marker = marker;
        _marker.addEventListener("click", function () {
          this.openInfoWindow(_iw);
        });
        _iw.addEventListener("open", function () {
          _marker.getLabel().hide();
        })
        _iw.addEventListener("close", function () {
          _marker.getLabel().show();
        })
        label.addEventListener("click", function () {
          _marker.openInfoWindow(_iw);
        })
        if (!!json.isOpen) {
          label.hide();
          _marker.openInfoWindow(_iw);
        }
      })()
    }
  }
  //创建InfoWindow
  function createInfoWindow(i) {
    var json = markerArr[i];
    var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>" + json.content + "</div>");
    return iw;
  }
  //创建一个Icon
  function createIcon(json) {
    var icon = new BMap.Icon("http://map.baidu.com/image/us_mk_icon.png", new BMap.Size(json.w, json.h), { imageOffset: new BMap.Size(-json.l, -json.t), infoWindowOffset: new BMap.Size(json.lb + 5, 1), offset: new BMap.Size(json.x, json.h) })
    return icon;
  }

  initMap();//创建和初始化地图
})

function getDate() {
  var myDate = new Date();
  return myDate.getFullYear();
}

function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var seperator2 = ":";
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
    + " " + date.getHours() + seperator2 + date.getMinutes()
  return currentdate;
}

